mk/<template>
  <div class="bgc">
    <el-form :model="form" :rules="rules" ref="form">
      <el-form-item prop="name">
        <el-input
          v-model="form.name"
          type="text"
          placeholder="请输入手机号"
          prefix-icon="iconfont icon-shouji"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          type="password"
          placeholder="请输入密码"
          prefix-icon="iconfont icon-jiesuo"
          suffix-icon="iconfont icon-yanjing_bi"
        ></el-input>
      </el-form-item>
      <el-form-item prop="yanzheng">
        <el-input
          v-model="form.yanzheng"
          prefix-icon="iconfont icon-anquan"
          placeholder="请输入验证码"
        >
          <div slot="append"><img :src="url" alt="" class="zymimg" /></div>
        </el-input>
      </el-form-item>
      <el-button type="primary" @click="onSubmit, submitForm('form')"
        >登陆</el-button
      >
      <div class="icon">
        <img src="./img/logo.595745bd.png" alt="" />
      </div>
    </el-form>
  </div>
</template>

<script>
import { getYZMapi } from "@/api";
export default {
  data() {
    return {
      form: {
        name: "admin",
        password: "admin",
        yanzheng: "",
        num: "",
      },
      url: "",
      rules: {
        name: [
          { required: true, message: "请输入正确的手机号", trigger: "blur" },
          // {
          //   pattern: /^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$/,
          //   message: "手机号格式不对",
          //   trigger: "blur",
          // },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          // {
          //   pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{5,8}$/,
          //   message: "请输入以字母开的5-8位数字或字母",
          //   trigger: "blur",
          // },
        ],
        yanzheng: [
          { required: true, message: "请输入验证码", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //登录
    onSubmit() {},
    //通过element的表单中的validate方法，判断表单中的校验是否全部通过
    submitForm(form) {
      this.$refs[form].validate(async (valid) => {
        if (valid) {
          // alert("验证通过!");
          console.log(this.form);
          try {
            const res = await this.$store.dispatch("loginAciton", {
              name: this.form.name,
              password: this.form.password,
              yanzheng: this.form.yanzheng,
              num: this.form.num,
            });
            if (res.data.success) {
              this.$router.push("/layout");
            }
          } catch (error) {
            console.log(error);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    random() {
      this.form.num = Math.floor(Math.random() * 10);
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  async created() {
    this.random();
    const res = await getYZMapi(this.form.num);
    console.log(res);
    this.url = res.config.url;
  },
};
</script>

<style lang="scss" scoped>
.bgc {
  position: relative;
  width: 100%;
  height: 100%;
  background-image: url("./img/background.be4fae7d.png");
  background-repeat: no-repeat;
  background-size: cover;
}
.el-form {
  position: absolute;
  width: 448px;
  height: 312px;
  top: 50%;
  left: 50%;
  margin-top: -194px;
  margin-left: -259px;
  padding: 76px 35px 0;
  background: #fff;
  box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
  border-radius: 10px;
}
.el-form-item__content {
  /* line-height: 100%; */
  width: 446px;
  height: 52px;
}
.el-input__inner {
  padding: 12px 5px 12px 15px;
  height: 52px;
}
.el-input__prefix {
  left: 10px;
}
.el-button--primary {
  width: 100%;
  height: 52px;
  background: linear-gradient(262deg, #2e50e1, #6878f0);
  border-radius: 8px;
}
.icon {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  top: -46px;
}
.icon img {
  width: 96px;
  height: 96px;
}
</style>